<?php

/* 
 * Project: colobok
 * and open the template in the editor.
 * @author: Sviatoslav Mysachenko <svyatoslavik@gmail.com>
 */

use map\Bounds;
class Item extends CActiveRecord{
    public $attributes = array();

    /**
     * Returns the static model of the specified AR class.
     * @return CActiveRecord the static model class
     */
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }
 
    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return 'items';
    }
    
    public function primaryKey()
    {
        return 'id';
        // For composite primary key, return an array like the following
        // return array('pk1', 'pk2');
    }
 
    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        return array(
//            array('lat','length','max'=>255),
//            array('lng','length','max'=>8),
//            array('lat, lng', 'required'),
        );
    }
    public static function getByBounds(Bounds $bounds){
        $query = '(lat BETWEEN :swLat AND :neLat) AND ';
        if ($bounds->nordEast->lng < $bounds->southWest->lng) {
            $query.='((lng BETWEEN :swLng AND 180.0) OR (lng BETWEEN -180.0 AND :neLng))';
        }
        else{
            $query.='(lng BETWEEN :swLng AND :neLng)';
        }
        return self::model()->findAll($query, array(
            'swLat' => $bounds->southWest->lat,
            'neLat' => $bounds->nordEast->lat,
            'swLng' => $bounds->southWest->lng,
            'neLng' => $bounds->nordEast->lng
        ));
    }
    public static $blockSize = array(
        'lat'  => 32.0,
        'lng'  => 48.0 
    );
    public static function getBlockSize($zoom)
    {
        $rect = array();
        foreach (array('lat', 'lng') as $k) {
            $rect[$k] = round(self::$blockSize[$k] / (1 << $zoom), 4);
            $rect[$k.'_half'] = round($rect[$k] / 2, 4);
        }
        return $rect;
    }
    public function loadGroupedMarkers(Bounds $bounds, $zoom){
       
//        $this->attributeDefaults= array('counts'=>0);
        $block = self::getBlockSize($zoom);
        $query = '(lat BETWEEN :swLat AND :neLat) AND ';
        if ($bounds->nordEast->lng < $bounds->southWest->lng) {
            $query.='((lng BETWEEN :swLng AND 180.0) OR (lng BETWEEN -180.0 AND :neLng))';
        }
        else{
            $query.='(lng BETWEEN :swLng AND :neLng)';
        }
        // FOR POSTGRE
        return $this->findAll(
                array(
                    'select'    => 'max(id) as id, max(lat) as lat, max(lng) as lng, count(id) as counts,  FLOOR( lat / :bLat ) AS grp_lat,
                                        FLOOR( lng / :bLng ) AS grp_lng',
                    'group'     => 'grp_lat, grp_lng',
                    'condition' => $query,
                    'params'    => array(
                                        'swLat' => $bounds->southWest->lat,
                                        'neLat' => $bounds->nordEast->lat,
                                        'swLng' => $bounds->southWest->lng,
                                        'neLng' => $bounds->nordEast->lng,
                                        'bLat'  => $block['lat'],
                                        'bLng'  => $block['lng']
                                    )
                ));
        
        // FOR MYSQL
        return $this->findAll(
                array(
                    'select'    => '*, count(id) as counts,  FLOOR( lat / :bLat ) AS grp_lat,
                                        FLOOR( lng / :bLng ) AS grp_lng',
                    'group'     => 'grp_lat, grp_lng',
                    'condition' => $query,
                    'params'    => array(
                                        'swLat' => $bounds->southWest->lat,
                                        'neLat' => $bounds->nordEast->lat,
                                        'swLng' => $bounds->southWest->lng,
                                        'neLng' => $bounds->nordEast->lng,
                                        'bLat'  => $block['lat'],
                                        'bLng'  => $block['lng']
                                    )
                ));
    }
//    public function populateRecord($attributes, $callAfterFind = true) {
////        print_r($attributes);die('hahaha');
//        $this->fullattributes = $attributes;
//        return parent::populateRecord($attributes, $callAfterFind);
//    }
    protected function instantiate($attributes){
        $mod = parent::instantiate($attributes);
        $mod->attributes = $attributes;
        return $mod;
    }

}